package com.teamall.web.qzgl.controller;

import com.teamall.core.common.JModelAndView;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.Utils;
import com.teamall.web.app.vo.FamilyHelpVO;
import com.teamall.web.app.vo.FamilyReplyVO;
import com.teamall.web.base.db.po.TownInfoPO;
import com.teamall.web.base.db.po.VillageInfoPO;
import com.teamall.web.base.service.ITownInfoService;
import com.teamall.web.base.service.IVillageInfoService;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.family.db.po.FamilyHelpPO;
import com.teamall.web.family.db.po.FamilyReplyPO;
import com.teamall.web.family.service.IFamilyHelpService;
import com.teamall.web.family.service.IFamilyReplyService;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-30 20:13
 **/
@Controller
@RequestMapping("/qzgl")
public class QzglController {
    @Autowired
    private ITownInfoService townInfoService;
    @Autowired
    private IVillageInfoService villageInfoService;
    @Autowired
    private IFamilyHelpService familyHelpService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IFamilyReplyService familyReplyService;

    @RequestMapping("list")
    public ModelAndView list() throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/qzgl/qzgl_main");
        List<TownInfoPO> townInfoPOList = townInfoService.selectByArg(new TownInfoPO());
        List<VillageInfoPO> villageInfoPOList = villageInfoService.selectByArg(new VillageInfoPO());
        for (TownInfoPO townInfoPO : townInfoPOList) {
            for (VillageInfoPO villageInfoPO : villageInfoPOList) {
                if (townInfoPO.getTownId() == villageInfoPO.getTownId()) {
                    townInfoPO.getVillageInfoPOList().add(villageInfoPO);
                }
            }
        }
        modelAndView.addObject("townInfoPOList", townInfoPOList);
        return modelAndView;
    }

    @RequestMapping("qzglList")
    public ModelAndView qzglIndex(Integer villageId, Integer townId) throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/qzgl/qzgl_list");
        List<FamilyHelpPO> familyHelpPOList = familyHelpService.selectByArg(new FamilyHelpPO());
        modelAndView.addObject("familyHelpPOList", familyHelpPOList);
        modelAndView.addObject("villageId", null == villageId ? "-1" : villageId);
        modelAndView.addObject("townId", null == townId ? "-1" : townId);
        return modelAndView;
    }

    @RequestMapping("helpMain")
    public ModelAndView helpMain(Integer villageId, Integer townId, Integer helpState, String content) throws BaseAppException {
        FamilyHelpPO paramPo = new FamilyHelpPO();
        paramPo.setVillageId(villageId);
        if (Utils.notEmpty(townId) && townId > 0) {
            paramPo.setTownId(townId);
        }
        paramPo.setHelpState(helpState);
        paramPo.setHelpContent(content);
        ModelAndView modelAndView = new JModelAndView("/qzgl/helpMain");
        List<FamilyHelpPO> familyHelpPOList = familyHelpService.selectByArg(paramPo);
        List<FamilyHelpVO> voList = new ArrayList<FamilyHelpVO>();
        for (FamilyHelpPO familyHelpPO : familyHelpPOList) {
            FamilyHelpVO vo = new FamilyHelpVO();
            BeanUtils.copyProperties(familyHelpPO, vo);
            SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(familyHelpPO.getHelpStaff());
            vo.setUserName(sysUserPO.getUserName());
            vo.setHeadImgUrl(sysUserPO.getHeadImgUrl());
            voList.add(vo);
        }
        modelAndView.addObject("familyHelpPOList", voList);
        modelAndView.addObject("villageId", villageId);
        modelAndView.addObject("townId", townId);
        modelAndView.addObject("helpState", helpState);
        modelAndView.addObject("content", content);
        return modelAndView;
    }

    @RequestMapping("/replyList")
    @ResponseBody
    public ResResult replyList(Integer helpId) throws BaseAppException {
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(helpId)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
        }
        FamilyReplyPO po = new FamilyReplyPO();
        po.setHelpId(helpId);
        List<FamilyReplyPO> familyReplyPOList = familyReplyService.selectByArg(po);
        List<FamilyReplyVO> familyReplyVOList = new ArrayList<FamilyReplyVO>();
        for (FamilyReplyPO replyPO : familyReplyPOList) {
            FamilyReplyVO familyReplyVO = new FamilyReplyVO();
            BeanUtils.copyProperties(replyPO, familyReplyVO);
            SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(replyPO.getUserId());
            familyReplyVO.setHeadImgUrl(sysUserPO.getHeadImgUrl());
            familyReplyVO.setUserName(sysUserPO.getUserName());
            familyReplyVOList.add(familyReplyVO);
        }
        resResult.setRspData(familyReplyVOList);
        return resResult;
    }
}
